package com.situ.ssm.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.situ.ssm.entity.Course;
import com.situ.ssm.entity.CourseExample;
import com.situ.ssm.entity.CourseExample.Criteria;
import com.situ.ssm.mapper.CourseMapper;
import com.situ.ssm.service.ICourseService;
import com.situ.ssm.vo.PageBean;
import com.situ.ssm.vo.CourseSearchCondition;

@Service
public class CourseServiceImpl implements ICourseService{
	@Autowired
	private CourseMapper courseMapper;

	@Override
	public List<Course> list() {
		return courseMapper.selectByExample(new CourseExample());
	}

	@Override
	public boolean insert(Course course) {
		int count = courseMapper.insert(course);
		return count == 1 ? true : false;
	}

	@Override
	public PageBean<Course> getPageBean(CourseSearchCondition condition) {
		PageBean<Course> pageBean = new PageBean<>();
		pageBean.setPageNo(condition.getPageNo());
		pageBean.setPageSize(condition.getPageSize());
		//totalCount
		CourseExample courseExample = new CourseExample();
		Criteria criteria = courseExample.createCriteria();
		if (StringUtils.isNotEmpty(condition.getName())) {
			criteria.andNameLike("%" + condition.getName() + "%");
		}
		Integer totalCount = courseMapper.countByExample(courseExample);
		pageBean.setTotalCount(totalCount);
		// totalPage
		Integer totalPage = (int) Math.ceil((double)totalCount / condition.getPageSize());
		pageBean.setTotalPage(totalPage);
		// list
		// PageHelper插件实现分页
		PageHelper.startPage(condition.getPageNo(), condition.getPageSize());
		List<Course> list = courseMapper.selectByExample(courseExample);
		pageBean.setList(list);
		
		return pageBean;
	}

}
